{ Statement: http://informatics.mccme.ru/moodle/mod/statements/view3.php?chapterid=1216
  Verdict: Accepted
}
uses
	SysUtils, Math;
 
var
	i, ls, n, cc : longint;
	a, b : array[0..1000010] of int64;
	s : string;
	ans : int64;
 
procedure sort(l, r : longint);
 
var
	i, j, x, y : longint;
 
begin
	i := l;
	j := r;
	x := a[(l + r) shr 1];
	repeat
		while a[i] < x do inc(i);
		while a[j] > x do dec(j);
		if i <= j then begin
			y := a[i];
			a[i] := a[j];
			a[j] := y;
			inc(i);
			dec(j);
		end;
	until i > j;
	if l < j then sort(l, j);
	if i < r then sort(i, r);
end;
 
begin
	reset(input,'input.txt');
	rewrite(output,'output.txt');
	readln(n);
	readln(S);
	ls := length(s);
	fillchar(a, sizeof(a), 0);
	a[0] := 0;
	for i := 1 to ls do
		if s[i] = 'b' then a[i] := a[i - 1] + 1 else a[i] := a[i - 1] - 1;
	sort(1, n);
	fillchar(b, sizeof(b), 0);
	cc := 0;
	b[0] := 1;
	for i := 1 to n do
		if a[i] = 0 then inc(b[0]) else
		if a[i] = a[i - 1] then inc(b[cc]) else begin
			inc(cc);
			b[cc] := 1;
		end;
	ans := 0;
	for i := 0 to cc do
		inc(ans, b[i] * (b[i] - 1) shr 1);
	write(ans);
end.